package com.caifeng.mynotepad;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

/**
 * 记事本
 * 保存 清空 长按删除 修改
 * SQLitedatabase
 */
public class Notepad extends AppCompatActivity {
    ListView listView;
    ImageView add;
    List<NotepadBran> list;
    SQLiteHelper mSqLiteHelper;
    NotepadAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad);
        listView = findViewById(R.id.l1);
        add = findViewById(R.id.add_note);
        //点击添加记事
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent addNoteIntent=new Intent(Notepad.this,RecordActivity.class);
                startActivityForResult(addNoteIntent,1);
            }
        });
        initData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1 && resultCode==2){
            showQueryData();
        }
    }

    //显示所有数据
    private void showQueryData(){
        if(list!=null){
            list.clear();
        }
        list=mSqLiteHelper.query();
        adapter=new NotepadAdapter(this,list);
        listView.setAdapter(adapter);
    }

    protected void initData(){
        mSqLiteHelper=new SQLiteHelper(this);//创建数据库
        showQueryData();
        //单击进入详细页面并传值
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NotepadBran notepadBran=list.get(position);
                Intent dataIntent=new Intent(Notepad.this,RecordActivity.class);
                dataIntent.putExtra("id",notepadBran.getId());
                dataIntent.putExtra("content",notepadBran.getNotepadContent());
                dataIntent.putExtra("time",notepadBran.getNotepadTime());
                Notepad.this.startActivityForResult(dataIntent,1);

            }
        });
        //长按删除
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog alertDialog;//弹出窗口
                AlertDialog.Builder builder=new AlertDialog.Builder(Notepad.this)
                        .setMessage("是否删除")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                NotepadBran notepadBran=list.get(position);
                                if(mSqLiteHelper.deleteData(notepadBran.getId())){
                                    list.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(Notepad.this,"删除成功", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog=builder.create();
                alertDialog.show();
                return true;
            }
        });

    }


}